package weather;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.alibaba.fastjson.JSONObject;
import com.show.api.ShowapiRequest;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by sirius_swu on 2017/3/14.
 */
public class GpsSearch {
    private LocationManager locationManager;
    private String locationProvider;
    private Location location;
    private String locationValue;
    JSONObject jsonObject;

    public GpsSearch() {

    }

    public GpsSearch(Context context) {
        Location location2 = getLocation(context);
        double lat = location2.getLatitude();
        double lng = location2.getLongitude();
        System.out.println("经度："+lat);
        System.out.println("纬度："+lng);
        ShowapiRequest req = new ShowapiRequest("http://ali-weather.showapi.com/gps-to-weather","68ff8fb05e4e4952b162405a4ed97f0a");
        byte b[] = req.addTextPara("from","1") .addTextPara("lat", String.valueOf(lat)).addTextPara("lng", String.valueOf(lng)).addTextPara("need3HourForcast", "1").addTextPara("needIndex", "1").addTextPara("needAlarm", "1").addTextPara("needMoreDay", "0").getAsByte();
        //打印返回头
        Map map = req.getRes_headMap();
        Iterator it = map.keySet().iterator();
        while (it.hasNext()) {
            Object k = it.next();
            System.out.println(k + "          " + map.get(k));
        }
        //打印返回体
        String	res = null;
        try {
            res = new String(b,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(res);
        //下面是使用阿里的fastjson包解析。如果不需要，请使用自己的解析包
        jsonObject = JSONObject.parseObject(res);
        System.out.println(jsonObject.getJSONObject("showapi_res_body"));
    }

    private Location getLocation(Context context) {
        // 获取地理位置管理器
        locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        // 获取所有可用的位置提供器
        List<String> providers = locationManager.getProviders(true);
        if (providers.contains(LocationManager.GPS_PROVIDER)) {
            // 如果是GPS
            locationProvider = LocationManager.GPS_PROVIDER;
        } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
            // 如果是NetWork
            locationProvider = LocationManager.NETWORK_PROVIDER;
        } else {
            System.out.println("无可用位置提供器！");
            location = null;
        }
        // 获取Location
        location = locationManager.getLastKnownLocation(locationProvider);
        if (location != null) {
            // 不为空，返回地理位置经纬度
            return location;
        }
        // 监视地理位置变化
        locationManager.requestLocationUpdates(locationProvider, 3000, 1, locationListener);
        return location;
    }

    /*
    * LocationListener监听器
    * 参数：地理位置提供器、监听位置变化的时间间隔、位置变化距离间隔、LocationListener监听器
    * */
    LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location1) {
            // 如果位置发生变化
            location = location1;
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }

    };

    /*
    * 返回Json
    * */
    public JSONObject retJson() {
        return jsonObject;
    }
}
